package com.voxel.sense.infomsg.module.user.staff;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.com.common.utils.L;
import com.example.com.common.utils.ListUtil;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.ConditionBean;
import com.voxel.sense.infomsg.interfaces.listener.OnDropMenuButtonListener;
import com.voxel.sense.infomsg.module.common.VRecyclerActivity;
import com.voxel.sense.infomsg.module.common.dropmenu.DropPickMenuBuilder;
import com.voxel.sense.infomsg.module.common.dropmenu.DropPickMenuManager;
import com.voxel.sense.infomsg.module.user.audit.AuditUserActivity;
import com.voxel.sense.infomsg.module.user.search.SearchUserActivity;
import com.voxel.sense.infomsg.module.user.staff.details.StaffDetailsActivity;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;
import com.voxel.sense.infomsg.view.dropmenu.DropDownMenu;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.api.response.StaffEntity;
import cyy.example.com.facotry.share.SPEngine;

/**
 * 人员列表 audit
 */
public class StaffListActivity extends VRecyclerActivity<StaffContract.Presenter, StaffEntity.SysUserListBean>
  implements StaffContract.View, OnDropMenuButtonListener {

  @BindView(R.id.btn_search_left)
  ImageView btn_search_left;//返回icon
  @BindView(R.id.tv_search)
  TextView tv_search; //搜索框
  //顶部搜索框，右边按钮
  @BindView(R.id.btn_right)
  TextView btn_right;
  @BindView(R.id.drop_down_menu)
  DropDownMenu drop_down_menu;

  private RecyclerView recycle_view;//用户列表

  //是否首次进入
  private boolean isFirstEnter = true;
  //加载类型
  private String mLoadStyle = Constants.FORMAL_USER_TYPE;
  private StaffAdapter mStaffAdapter;
  private DropPickMenuManager mMenuManager;

  public static void show(Context context) {
    show(context, Constants.FORMAL_USER_TYPE);
  }

  /**
   * @param context
   * @param type    正式用户列表 Constants.FORMAL_USER_TYPE  待审核  Constants.INFORMAL_USER_TYPE
   */
  public static void show(Context context, String type) {
    if (Constants.FORMAL_USER_TYPE.equals(type) || Constants.INFOMARL_USER_YPE.equals(type)) {
      Intent intent = new Intent(context, StaffListActivity.class);
      intent.putExtra(IntentConstants.STRING_TYPE, type);
      context.startActivity(intent);
    }
  }

  protected int getContentLayoutId() {
    return R.layout.activity_staff_list;
  }

  @Override
  protected StaffContract.Presenter initPresenter() {
    return new StaffPresenter(this, mLoadStyle);
  }

  private String st1;
  @Override
  protected boolean initArgs(Bundle bundle) {
    String status = getIntent().getStringExtra(IntentConstants.STRING_TYPE);
    status = (TextUtils.isEmpty(status)) ? Constants.FORMAL_USER_TYPE : status;
    st1 = status;
    mLoadStyle = status;
    return super.initArgs(bundle);
  }

  @Override
  protected void initWidget() {
    super.initWidget();
//    if (Constants.FORMAL_USER_TYPE.equals(mLoadStyle)) {
//      btn_right.setVisibility(View.VISIBLE);
//      btn_right.setText("审核");
//    }
    String isCheck = SPEngine.getSPEngine().getUserInfo().getDistributing();
    if ("1".equals(isCheck) && !st1.equals(Constants.INFOMARL_USER_YPE)) {
      btn_right.setVisibility(View.VISIBLE);
      btn_right.setText("审核");
    }else {
      btn_right.setVisibility(View.INVISIBLE);
    }
    mLoadStyle = isCheck;
    initContentUI();
  }

  @Override
  protected void initData() {
    super.initData();
    //初始化用户列表
    //初始化条件筛选菜单栏列表
    //是否有审核权限

    if (isFirstEnter) {
      refresh_layout.autoRefresh();//第一次进入触发自动刷新，演示效果
    }
    tv_search.setText(getString(R.string.title_search_user));
  }

  @Override
  protected void initEven() {
    super.initEven();
    //滑动监听
    refresh_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
      @Override
      public void onLoadMore(RefreshLayout refreshLayout) {
        if (NetworkUtils.isConnected()) {
          mPresenter.doLoadMoreData();
        }
        else {
          showEmptyView(getString(R.string.network_unavailable), true);
          refresh_layout.finishLoadMore();
        }
      }
    });

    //下拉刷新控件监听
    refresh_layout.setOnRefreshListener(new OnRefreshListener() {
      @Override
      public void onRefresh(RefreshLayout refreshLayout) {
        if (NetworkUtils.isConnected()) {
          //重新刷新，需要改变状态，可以加载更多数据
          refresh_layout.setNoMoreData(false);
          mPresenter.doLoadData();
        }
        else {
          refresh_layout.finishRefresh();
          showEmptyView(getString(R.string.network_unavailable), true);
        }
      }
    });

    mStaffAdapter.setListener(new RecyclerAdapter.AdapterListenerImpl<StaffEntity.SysUserListBean>() {
      @Override
      public void onItemClick(RecyclerAdapter.ViewHolder holder, StaffEntity.SysUserListBean bean) {
        super.onItemClick(holder, bean);
        if (Constants.INFOMARL_USER_YPE.equals(st1)) {
          AuditUserActivity.show(StaffListActivity.this, bean);
        }
        else {
          StaffDetailsActivity.show(StaffListActivity.this, bean, bean.getUserId());
        }
      }
    });

    btn_right.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //跳转审核界面
        show(StaffListActivity.this
          , Constants.INFOMARL_USER_YPE);
      }
    });
  }

  private void initContentUI() {
    //初始化公司列表控件
    View view = LayoutInflater.from(this).inflate(R.layout.layout_recycler_emptty, null);
    refresh_layout = view.findViewById(R.id.refresh_layout);
    recycle_view = view.findViewById(R.id.recycle_view);
    empty_view = view.findViewById(R.id.empty_view);
    empty_view.bind((View) refresh_layout);
    recycle_view.setLayoutManager(new LinearLayoutManager(this));
    mStaffAdapter = new StaffAdapter();

    LinearItemDecoration linearItemDecoration = new LinearItemDecoration(R.drawable.recycle_divider_10dp);
    linearItemDecoration.setDrawFirstItem(true).setDrawVerticalModelLeftAndRightBorder(true, true);
    recycle_view.addItemDecoration(linearItemDecoration);
    recycle_view.setAdapter(mStaffAdapter);
    CreateMenu(view);
  }

  /**
   * 创建筛选菜单
   *
   * @param view
   */
  private void CreateMenu(View view) {
    //初始化筛选栏管理工具
    DropPickMenuBuilder menuBuilder = new DropPickMenuBuilder(drop_down_menu, view);

    //创建列表
    mMenuManager = menuBuilder.buildDepartmentList()
      .builRoleType()
      .addResetAndSubmitListener(this)
      .addItemOnclickListener(new RecyclerAdapter.AdapterListenerImpl<ConditionBean>() {
        @Override
        public void onItemClick(RecyclerAdapter.ViewHolder holder, ConditionBean o) {
          super.onItemClick(holder, o);
          //点击事件
          if (o.isSelect()) {
            mPresenter.putSearTarget(o.getType(), o.getTipCode());
          }
          else {
            mPresenter.removeSearchData(o.getType(), o.getTipCode());
          }
        }
      })
      .build();
    mPresenter.doSearchCondition();
  }

  @OnClick({R.id.tv_search, R.id.btn_search_left})
  void OnClickEvent(View view) {
    switch (view.getId()) {
      case R.id.tv_search:
        SearchUserActivity.show(StaffListActivity.this, mLoadStyle);
        break;
      case R.id.btn_search_left:
        onBackPressed();
        break;
    }
  }

  @Override
  public void onRefreshMenu(List<ConditionBean> userType, List<ConditionBean> actor) {
    mMenuManager.setDeparmentTypeData(userType);
    mMenuManager.setRoleTypeData(actor);
  }

  @Override
  public void onRefreshFinish() {
    super.onRefreshFinish();
    if (isFirstEnter) {
      isFirstEnter = false;
    }
    L.d(" mStaffAdapter.getItems().size():" + mStaffAdapter.getItems().size());
  }

  @Override
  public RecyclerAdapter<StaffEntity.SysUserListBean> getRecyclerViewAdapter() {
    return mStaffAdapter;
  }

  /**
   * 清除筛选条件
   *
   * @param type 类型
   */
  @Override
  public void onResetClick(String type) {
    mPresenter.doResetCondition(type);
  }

  /**
   * 提交查询
   */
  @Override
  public void onSubmitClick(String type) {
    //若占位布局显示了，会遮挡住下拉控件，需先隐藏占位空布局
    empty_view.triggerOk();
    refresh_layout.autoRefresh();
//        mPresenter.doLoadData();
    drop_down_menu.closeMenu();
  }

}
